<template>
  <div class="service-container">
    <el-card class="table-card">
      <template #header>
        <div class="table-header">
          <h3>装修备案管理</h3>
          <div class="search-toggle-area">
            <el-button class="search-toggle-button" @click="toggleSearchBar">
              <el-icon><Search /></el-icon>
            </el-button>
            <el-form :inline="true" :model="queryParams" class="search-form" :class="{'is-hidden': !showSearchBar}" ref="queryForm">
              <el-form-item label="状态">
                <el-select v-model="queryParams.status" placeholder="请选择状态" clearable style="width: 160px;">
                  <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="handleQuery">查询</el-button>
                <el-button @click="resetQuery">重置</el-button>
              </el-form-item>
            </el-form>
          </div>
        </div>
      </template>

      <div class="search-bar" v-if="false">
        <el-form :model="queryParams" ref="queryForm" :inline="true" label-width="68px">
          <el-form-item label="状态" prop="status">
            <el-select v-model="queryParams.status" placeholder="请选择状态" clearable size="small" @change="handleQuery" style="width: 180px;">
              <el-option v-for="item in statusOptions" :key="item.value" :label="item.label" :value="item.value"></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="small" @click="handleQuery">查询</el-button>
            <el-button size="small" @click="resetQuery">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <el-table 
        v-loading="loading" 
        :data="decorationList"
        border
        stripe
        :header-cell-style="{background:'#f5f7fa', color:'#606266'}"
      >
        <el-table-column label="申请ID" align="center" prop="application_id" width="100"></el-table-column>
        <el-table-column label="申请人" align="center" prop="applicant_name" width="120"></el-table-column>
        <el-table-column label="装修单元" align="center" prop="unit_info" width="250"></el-table-column>
        <el-table-column label="施工单位" align="center" prop="construction_company" width="250"></el-table-column>
        <el-table-column label="申请日期" align="center" prop="application_date"></el-table-column>
        <el-table-column label="状态" align="center" prop="status">
            <template #default="scope">
                <el-tag :type="getStatusTagType(scope.row.status)" size="small">{{ scope.row.status }}</el-tag>
            </template>
        </el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button size="small" type="primary" link @click="handleView(scope.row)">查看</el-button>
          </template>
        </el-table-column>
      </el-table>
      
      <el-empty v-if="decorationList.length === 0 && !loading" description="暂无装修备案数据" class="empty-data-placeholder"></el-empty>

      <div class="pagination-container">
        <el-pagination 
          v-show="total > 0" 
          :total="total" 
          v-model:current-page="queryParams.current" 
          v-model:page-size="queryParams.size" 
          @size-change="getList" 
          @current-change="getList"
          :page-sizes="[10, 20, 50, 100]"
          layout="total, sizes, prev, pager, next, jumper"
        ></el-pagination>
      </div>
    </el-card>

    <!-- 详情对话框 -->
    <el-dialog :title="dialogTitle" v-model="dialogVisible" width="700px" append-to-body>
      <div v-if="currentApplication" v-loading="detailLoading">
        <el-descriptions border :column="2">
          <el-descriptions-item label="申请人">{{ currentApplication.applicant_name }}</el-descriptions-item>
          <el-descriptions-item label="申请ID">{{ currentApplication.application_id }}</el-descriptions-item>
          <el-descriptions-item label="装修单元">{{ currentApplication.unit_info_str }}</el-descriptions-item>
          <el-descriptions-item label="申请日期">{{ currentApplication.application_date }}</el-descriptions-item>
          <el-descriptions-item label="施工单位">{{ currentApplication.construction_company }}</el-descriptions-item>
          <el-descriptions-item label="预计工期">{{ currentApplication.estimated_duration }} 天</el-descriptions-item>
          <el-descriptions-item label="当前状态">
            <el-tag :type="getStatusTagType(currentApplication.status)">{{ currentApplication.status }}</el-tag>
          </el-descriptions-item>
        </el-descriptions>
        
        <h4>施工人员信息</h4>
        <el-table :data="currentApplication.construction_staff_info" border size="small">
          <el-table-column property="name" label="姓名"></el-table-column>
          <el-table-column property="phone" label="联系电话"></el-table-column>
          <el-table-column property="id_card" label="身份证号"></el-table-column>
        </el-table>

        <h4>装修方案</h4>
        <div v-if="currentApplication.design_plan_url && currentApplication.design_plan_url.length > 0">
          <el-image v-for="(url, index) in currentApplication.design_plan_url" :key="index" style="width: 100px; height: 100px; margin-right: 10px;" :src="url" :preview-src-list="currentApplication.design_plan_url"></el-image>
        </div>
        <div v-else>无</div>


        <div v-if="currentApplication.status === '审核中'">
            <h4 style="margin-top: 20px;">审批意见</h4>
            <el-input type="textarea" v-model="approvalOpinion" placeholder="请输入审批意见"></el-input>
        </div>
         <div v-else-if="currentApplication.approval_opinion">
             <h4 style="margin-top: 20px;">审批意见</h4>
            <p>{{ currentApplication.approval_opinion }}</p>
        </div>

      </div>
      <template #footer>
        <div class="dialog-footer">
          <el-button @click="dialogVisible = false">关闭</el-button>
          <div v-if="currentApplication && currentApplication.status === '审核中'">
            <el-button type="danger" @click="handleApprove('审核驳回')">驳回</el-button>
            <el-button type="primary" @click="handleApprove('审核通过')">通过</el-button>
          </div>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue';
import { getDecorationApplicationList, getDecorationApplicationDetail, approveDecorationApplication } from '@/api/services';
import { ElMessage } from 'element-plus';
import { Search } from '@element-plus/icons-vue'

const loading = ref(true);
const detailLoading = ref(false);
const decorationList = ref([]);
const total = ref(0);
const dialogVisible = ref(false);
const dialogTitle = ref('');
const currentApplication = ref(null);
const approvalOpinion = ref('');
const showSearchBar = ref(false);

const queryParams = reactive({
  current: 1,
  size: 10,
  status: '',
});

const statusOptions = ref([
  { value: '待审核', label: '待审核' },
  { value: '审核中', label: '审核中' },
  { value: '审核通过', label: '审核通过' },
  { value: '审核驳回', label: '审核驳回' },
  { value: '施工中', label: '施工中' },
  { value: '已完工', label: '已完工' },
  { value: '已验收', label: '已验收' },
]);

function getStatusTagType(status) {
    switch (status) {
        case '待审核': return 'info';
        case '审核中': return '';
        case '审核通过': return 'success';
        case '审核驳回': return 'danger';
        case '施工中': return 'warning';
        case '已完工': return 'success';
        case '已验收': return 'primary';
        default: return 'info';
    }
}

// 切换搜索栏显示状态
const toggleSearchBar = () => {
  showSearchBar.value = !showSearchBar.value;
};

function getList() {
  loading.value = true;
  getDecorationApplicationList(queryParams).then(response => {
    decorationList.value = response.data.data.list;
    total.value = response.data.data.total;
    loading.value = false;
  });
}

function handleQuery() {
  queryParams.current = 1;
  getList();
}

function resetQuery() {
  queryParams.status = '';
  handleQuery();
}

function handleView(row) {
  dialogVisible.value = true;
  dialogTitle.value = '装修备案详情';
  detailLoading.value = true;
  currentApplication.value = null;
  approvalOpinion.value = '';

  getDecorationApplicationDetail(row.application_id).then(response => {
    currentApplication.value = response.data.data;
    detailLoading.value = false;
  });
}

function handleApprove(status) {
    if(!approvalOpinion.value && status === '审核驳回') {
        ElMessage.error('请输入驳回意见');
        return;
    }

    const data = {
        status: status,
        approval_opinion: approvalOpinion.value
    };

    approveDecorationApplication(currentApplication.value.application_id, data).then(() => {
        ElMessage.success('操作成功');
        dialogVisible.value = false;
        getList();
    });
}

onMounted(() => {
  getList();
});
</script>

<style scoped>
.service-container {
  padding: 20px;
  background-color: #f8f9fa;
  min-height: calc(100vh - 80px);
}

.search-bar {
  margin-bottom: 20px;
  padding: 15px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.table-card {
  border-radius: 8px;
  box-shadow: 0 2px 12px 0 rgba(0,0,0,0.05);
}

.table-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.table-header h3 {
  margin: 0;
  font-size: 18px;
  font-weight: 600;
}

/* 搜索区域 */
.search-toggle-area {
  display: flex;
  flex-direction: row;
  align-items: center;
}

.search-toggle-button {
  font-size: 20px;
  color: #606266;
  align-self: center;
  margin-right: 15px;
  transition: all 0.2s;
  cursor: pointer;
  width: 36px;
  height: 36px;
  display: flex;
  justify-content: center;
  align-items: center;
  border-radius: 50%;
  background-color: rgba(0, 0, 0, 0.03);
  border: 1px solid rgba(0, 0, 0, 0.05);
  z-index: 10;
  margin-bottom: 0;
}

.search-toggle-button:hover {
  color: #409eff;
  background-color: rgba(64, 158, 255, 0.1);
  border-color: rgba(64, 158, 255, 0.2);
}

.search-form {
  max-width: 1000px;
  width: auto;
  overflow: hidden;
  transition: max-width 0.4s ease-in-out, opacity 0.4s ease-in-out, background-color 0.4s ease-in-out, box-shadow 0.4s ease-in-out, padding 0.4s ease-in-out, margin-left 0.4s ease-in-out, height 0.4s ease-in-out, min-height 0.4s ease-in-out;
  opacity: 1;
  background-color: transparent;
  border-radius: 8px;
  padding: 0 20px;
  box-shadow: none;
  pointer-events: auto;
  margin-left: 15px;
  white-space: nowrap;
  height: 35px;
  min-height: 35px;
}

.search-form.is-hidden {
  max-width: 0;
  opacity: 0;
  padding: 0;
  margin-left: 0;
  height: 0;
  min-height: 0;
  background-color: transparent;
  box-shadow: none;
  pointer-events: none;
}

/* 调整搜索表单项的底部外边距 */
:deep(.search-form .el-form-item) {
  margin-bottom: 0;
  margin-right: 15px;
  display: inline-flex;
  align-items: center;
}

:deep(.search-form .el-form-item__label) {
  padding-right: 8px;
}

:deep(.table-card .el-card__header) {
  background-color: #f9fafc;
  padding: 15px 20px;
}

:deep(.table-card .el-card__body) {
  padding: 20px;
}

.pagination-container {
  margin-top: 25px;
  display: flex;
  justify-content: flex-end;
  padding: 12px 15px;
  background-color: #f9fbfd;
  border-radius: 10px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.03);
}

.empty-data-placeholder {
  margin-top: 40px;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 10px;
}

/* 搜索框el-input优化 */
:deep(.el-input__inner) {
  border-color: #e0e5ed !important;
  transition: border-color 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  border-radius: 6px;
}

:deep(.el-input__inner:focus) {
  border-color: rgba(64, 158, 255, 0.4) !important;
}

:deep(.el-input__inner::placeholder) {
  color: #aebccf;
}

/* 按钮el-button优化 */
:deep(.el-button--primary) {
  background-color: rgba(64, 158, 255, 0.1) !important;
  color: #606266 !important;
  border-color: rgba(64, 158, 255, 0.2) !important;
  transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  border-radius: 6px;
}

:deep(.el-button--primary:hover) {
  background-color: rgba(64, 158, 255, 0.15) !important;
  border-color: rgba(64, 158, 255, 0.3) !important;
}

/* Default Button */
:deep(.el-button--default) {
  background-color: #ffffff !important;
  color: #606266 !important;
  border-color: #dcdfe6 !important;
  transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  border-radius: 6px;
}

:deep(.el-button--default:hover) {
  background-color: #f7f9fa !important;
  border-color: #c8d3dd !important;
}

/* Danger Button */
:deep(.el-button--danger) {
  background-color: rgba(245, 108, 108, 0.1) !important;
  color: #F56C6C !important;
  border-color: rgba(245, 108, 108, 0.2) !important;
  transition: all 0.2s cubic-bezier(0.645, 0.045, 0.355, 1);
  border-radius: 6px;
}

:deep(.el-button--danger:hover) {
  background-color: rgba(245, 108, 108, 0.15) !important;
  border-color: rgba(245, 108, 108, 0.3) !important;
}
</style> 